<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of JDatePagination
 *
 * @author chaporgin_a
 */
class VA_Custom_Helper_JDatePagination extends VA_Helper_JDatePagination {
    const ST_LIST = 'list';
    const ST_SELECT = 'select';
    public function Ribbon($options) {
        $style = self::GetOption('style', $options, 'list');
        switch ($style) {
            case self::ST_LIST:
                return parent::Ribbon($options);
            case self::ST_SELECT:
                return $this->SelectRibbon($options);
        }
        $knownStyles = array(self::ST_LIST, self::ST_SELECT);
        throw new Exception(sprintf('unknown style %s, must be one of %s',
        $style,
        $knownStyles));

    }
    protected function SelectRibbon($options = array()) {
        $defaultRange = self::GetOption('selectrange', $options, $this->defaultRange);
        $stopDates = $this->RibbonAsArray($options);
        $offsetUnit = self::GetOption('step', $options, 'months');
        $format = $this->getFormat($offsetUnit);
        $select = null;
        $form = $this->prepareForm($select);
        foreach($stopDates as $date) {
            $rangeStart = strtotime(date($format, $date));
            $rangeEnd = strtotime(sprintf('+1 %s', $offsetUnit), $rangeStart);
            $linkRange = $this->prepareLinkRange($rangeStart,$rangeEnd, $defaultRange);
            if(0 === count($this->paginator->findElementsInRange($rangeStart, $rangeEnd)))
                continue;
            $route = "&limit=1000&from=".$linkRange[0]."&till=".$linkRange[1];
            $select->setOption(date('Y',strtotime($linkRange[0])), JRoute::_($route));
            $isActive = (($this->paginator->getFromTimestamp() <= $date) && ($date < $this->paginator->getTillTimestamp()) );
            if($isActive) $select->addValue(JRoute::_($route));
        }
        $map = new VA_Form_Form_Mapper_XML();
        return $form;
    }
    /**
     *
     * @return VA_Form_Form
     */
    protected function prepareForm(&$select) {
        $form = new VA_Form_Form();
        $form->setGID('years');
        $form->setTitle(JText::_('select ribbon'));
        $select = new VA_Form_FormElement_Select();
        $form->addElement($select);
        $select->setLabel('год')->setHTMLTagAttribute('class', 'ribbonYears');
        $select->setName('path');
        $submit = new VA_Form_FormElement_Submit();
        $submit->setValue('отправить');
        $submit->setStyle('display', 'none');
        $form->addElement($submit);
        return $form;
    }
}
